<script>
import { defineComponent } from 'vue'
import axios from 'axios'

export default defineComponent({
  name: 'LoginView',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      try {
        this.loading = true
        const response = await axios.post('/api/auth/login', this.loginForm)
        if (response.data.code === 200) {
          // 存储token
          localStorage.setItem('token', response.data.data)
          // 跳转到首页
          this.$router.push('/')
        } else {
          // 显示错误信息
          alert(response.data.message)
        }
      } catch (error) {
        console.error('登录失败:', error)
        alert('登录失败，请重试')
      } finally {
        this.loading = false
      }
    }
  }
})
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">工厂管理系统</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-item">
          <input
            v-model="loginForm.username"
            type="text"
            placeholder="用户名"
            required
          >
        </div>
        <div class="form-item">
          <input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            required
          >
        </div>
        <button 
          type="submit" 
          :disabled="loading"
          class="login-button"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.login-box {
  width: 90%;
  max-width: 400px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 24px;
  color: #333;
  font-size: 24px;
}

.form-item {
  margin-bottom: 16px;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.login-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
</style> 